import { modifyScore, findScores } from '../services/fenzu';

export default {
  namespace: 'score',

  state: {
    scoreList: [],
  },

  effects: {
    *modifyScore({ payload, callback }, { select, call, put }) {
      const response = yield call(modifyScore, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *findScores({ payload, callback }, { select, call, put }) {
      const response = yield call(findScores, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: {
            scoreList: Array.isArray(response) ? response : [],
          },
        });
        if (callback) callback();
      }
    },
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clean(state, payload) {
      return {
        ...state,
        fenzuList: [],
        curZubieObj: {},
        zhixuList: [],
      };
    },
  },
};
